// BracketBoltForces.cpp : main project file.
#include "stdafx.h"

using namespace std;

ColBracket::ColBracket(int boltRows, double edgeDistance, double boltSpacing, double effectiveWidth,
						double boltDiameter, double AppliedLoad, double eccentricity) 
{
		n = boltRows;							//The number of two-bolt rows in the bracket. 
		P = AppliedLoad;						//The applied bracket loading
		e = eccentricity;						//The bracket eccentricity
		Le = edgeDistance;						//The distance from the bottom bolt to the bottom of the plate
		s = boltSpacing;						//The spacing between bolts, uniform
		be = effectiveWidth;					//The effective width
		db = boltDiameter;						//The bolt diameter
		int CalcForces = WriteForces();
		
};

int ColBracket::WriteForces() {
	ofstream logfile;
	logfile.open("logfile.txt");
	double d = Centroid();
	double Ix = CalcMomentofInertia();
	double *ymatrix = CalcYMatrix(d);
	double *AreaMatrix = CalcAreaMatrix(d);
	double *boltForceMatrix = new double[n];
	for (int i = 0; i < n; i++) {
		boltForceMatrix[i] = P * e * ymatrix[i] * AreaMatrix[i] / Ix / 2;
		logfile << "\nThe bolt force at row " << i + 1 << " is " << boltForceMatrix[i];
	};
	logfile.close();
	return 1;
};



double ColBracket::CalcCompArea(double d) {
	//This function returns the compression area by taking the effective width of the compression block and multiplying
	//it by its depth
	double CompArea = d*be;
	return CompArea;
};
double ColBracket::CalcCompy(double d) {
	//This function returns the distance of the centroid from the neutral axis
	double CompY = -d / 2;
	return CompY;
};
double ColBracket::CalcCompAy(double d){
	//This function returns the multiplication of the area of the compression block by its centroidal distance to the neutral axis
	double CompAy = CalcCompArea(d) * CalcCompy(d);
	return CompAy;
};
double ColBracket::CalcCompAyy(double d){
	//This function returns the multiplication of the area of the compression block by its centroidal distance to the neutral axis
	//squared.
	double CompAyy = CalcCompArea(d) * CalcCompy(d)* CalcCompy(d);
	return CompAyy;
};
double ColBracket::CalcSumArea(double d) {
	//This function returns the sum of the bolt areas that are in tension. Bolts in compression are excluded.
	double SumArea = 0;
	double *AreaMatrix = CalcAreaMatrix(d);
	for (int i = 0; i<n; i++) {
	SumArea = SumArea + AreaMatrix[i];
	};
	SumArea = SumArea + CalcCompArea(d);
	return SumArea;
};
double ColBracket:: CalcSumAy(double d) {
	//This function returns the sum of the bolt areas times its distance to the neutral axis.
	double SumAy = 0;
	double *AyMatrix = CalcAyMatrix(d);
	for (int i = 0; i<n; i++) {
	SumAy = SumAy + AyMatrix[i];
	};
	SumAy = SumAy + CalcCompAy(d);
	return SumAy;
};
double ColBracket::CalcSumAyy(double d) {
	double SumAyy = 0;
	double *AreaMatrix = CalcAreaMatrix(d);
	double *ymatrix = CalcYMatrix(d);
	double *AyyMatrix = CalcAyyMatrix(d);
	for (int i = 0; i<n; i++) {
	SumAyy = SumAyy + AyyMatrix[i];
	};
	SumAyy = SumAyy + CalcCompAyy(d);
	return SumAyy;
};
double ColBracket::CalcCentroidi(double d){
	double sumAy = CalcSumAy(d);
	double SumArea = CalcSumArea(d);
	double ybar = sumAy / SumArea;
	return ybar;
};
double ColBracket::abss(double a) {
	double ret;
	if (a < 0) { ret = -1 * a; }
	else
	{
	ret = a;
	};
	return ret;
};
double *ColBracket::boltLocationMatrix(){
	double *boltLocations;
	int i;
	boltLocations = new double[n];
	boltLocations[0] = Le;
	std::cout << "\nThe bolt locations are: " << boltLocations[0];

	for (i = 1; i<n; i++) {
	boltLocations[i] = Le + i*s;
	//std::cout << ", " << boltLocations[i];
	};
	std::cout << " From the bottom of the bracket.";
	return boltLocations;
};
double *ColBracket::CalcYMatrix(double d) {
	double *ymatrix;
	double globalLocation;
	double *boltLocations = boltLocationMatrix();
	ymatrix = new double[n];
	for (int i = 0; i < n; i++) {
	globalLocation = boltLocations[i];
	ymatrix[i] = globalLocation - d;
	std::cout << "\nThe y dimension at " << i << " is " << ymatrix[i];
	};
	return ymatrix;
};
double ColBracket::calcBoltArea() {
	double boltArea;
	boltArea = 3.1415927 * db*db / 4;
	return boltArea;
};
double *ColBracket::CalcAreaMatrix(double d) {
	double *areaMatrix;
	double boltArea;
	double *ymatrix = CalcYMatrix(d);
	boltArea = calcBoltArea() * 2;
	areaMatrix = new double[n];
	for (int i = 0; i < n; i++) {
	if (ymatrix[i] > 0) { areaMatrix[i] = boltArea; }
	else { areaMatrix[i] = 0; };
	std::cout << "\nThe area at " << i << " is " << areaMatrix[i];
	};
	return areaMatrix;
};
double *ColBracket::CalcAyMatrix(double d) {
	double *AyMatrix;
	double *ymatrix = CalcYMatrix(d);
	double *areaMatrix = CalcAreaMatrix(d);
	AyMatrix = new double[n];
	for (int i = 0; i < n; i++) {
	AyMatrix[i] = ymatrix[i] * areaMatrix[i];
	std::cout << "\nThe Ay at " << i << " is " << AyMatrix[i];
	};
	return AyMatrix;
};
double *ColBracket::CalcAyyMatrix(double d) {
	double *AyyMatrix;
	double *ymatrix = CalcYMatrix(d);
	double *areaMatrix = CalcAreaMatrix(d);
	AyyMatrix = new double[n];
	for (int i = 0; i < n; i++) {
	AyyMatrix[i] = ymatrix[i] * ymatrix[i] * areaMatrix[i];
	};
	return AyyMatrix;
};
double ColBracket::Centroid() {
	//This is a function that calculates the area and Ay matrices for a given d and then the centroid;

	//Initialize Local Variables
	double *ymatrix;
	double *areaMatrix;
	double *AyMatrix;
	double sumAy;
	double sumA;
	double tol = 0.001; //Tolerance for convergence of assumed and
	double diff = 1;
	double d = 0;
	
	while (abss(diff) > tol)
	{
	std::cout << "\nBased on d = " << d;
	//Generate the bolt locations for the presumed d
	ymatrix = CalcYMatrix(d);

	//Generate the Area matrix for the presumed d;
	areaMatrix = CalcAreaMatrix(d);

	//Generate the Ay Matrix for the presumed d;
	AyMatrix = CalcAyMatrix(d);

	//Calculate sum Ay for the presumed d;
	sumAy = CalcSumAy(d);

	//Calculate sumA for the presumed d;
	sumA = CalcSumArea(d);

	//Calculate the centroid location for the presumed d;
	diff = CalcCentroidi(d);
	d = d + diff;
		}
	return d;
	};
double ColBracket::calcCompIx(double d) {
	double CompI;
	CompI = d*d*d * be / 12;
	return CompI;
	};
double ColBracket::CalcMomentofInertia() {
	double d = Centroid();
	double I = CalcSumAyy(d) + calcCompIx(d);
	return I;
};
int ColBracket::BoltForces() {
	double d = Centroid();
	double Ix = CalcMomentofInertia();
	double *ymatrix = CalcYMatrix(d);
	double *AreaMatrix = CalcAreaMatrix(d);
	double *boltForceMatrix = new double[n];
	for (int i = 0; i < n; i++) {
		boltForceMatrix[i] = P * e * ymatrix[i] * AreaMatrix[i] / Ix / 2;
		cout << "\nThe bolt force at row " << i + 1 << " is " << boltForceMatrix[i];
	};
	return 1;
};


double AppliedLoading;
double eccentricity;
double boltDiameter;
int nbolts;
double edgeDistance;
double boltSpacing;
double effectiveWidth;


int main(array<System::String ^> ^args)
{
	;
	cout << "\nPlease enter the bolt diameter: ";
	cin >> boltDiameter;
	cout << "\nPlease enter the number of bolt rows: ";
	cin >> nbolts;
	cout << "\nPlease enter the vertical edge distance: ";
	cin >> edgeDistance;
	cout << "\nPlease enter the bolts spacing: ";
	cin >> boltSpacing;
	cout << "\nPlease enter the effective width: ";
	cin >> effectiveWidth;
	cout << "\nPlease enter the Applied Loading: ";
	cin >> AppliedLoading;
	cout << "\nPlease enter the bracket eccentricity: ";
	cin >> eccentricity;

	ColBracket Brackett(nbolts, edgeDistance, boltSpacing, effectiveWidth, boltDiameter,AppliedLoading,eccentricity);



	cout << "End";






    return 0;


}
